package com.itheima.sky.controller.front;

import com.itheima.sky.entity.Result;
import com.itheima.sky.pojo.AddressBook;
import com.itheima.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description AddressBookController
 * @Author huangbailin
 * @Date 2024-06-27
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增
     * @param addressBook
     * @param session
     * @return
     */
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook,HttpSession session){
//        Long userId = (Long) session.getAttribute("user");
        Long userId = null;
        try {
            userId = (Long) session.getAttribute("userId");
        } catch (ClassCastException e) {
            // 处理类型转换异常
        }
        addressBook.setUserId(userId);
        addressBook.setCreateUser(userId);
        addressBook.setUpdateUser(userId);
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBookService.save(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return Result.success(addressBook);
        } else {
            return Result.error("没有找到该对象");
        }
    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    public Result list(HttpSession session) {
        Long userId = (Long) session.getAttribute("userId");
        List<AddressBook> addressList = addressBookService.queryAddressList(userId);

        return Result.success(addressList);
    }

    /**
     * 查询用户默认地址
     * @param session
     * @return
     */
    @GetMapping("default")
    public Result getDefault(HttpSession session) {
        Long userId = (Long) session.getAttribute("userId");
        AddressBook addressBook = addressBookService.getDefaultAddress(userId);

        if (null == addressBook) {
            return Result.error("没有找到该对象");
        } else {
            return Result.success(addressBook);
        }
    }

    /**
     * 修改为默认地址
     * @param addressBook
     * @param session
     * @return
     */
    @PutMapping("default")
    public Result setDefault(@RequestBody AddressBook addressBook,HttpSession session) {
        Long userId = (Long) session.getAttribute("userId");
        addressBook.setUserId(userId);
        addressBookService.updateDefaultAddress(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 修改地址
     * @param addressBook
     * @param session
     * @return
     */
    @PutMapping
    public Result edit(@RequestBody AddressBook addressBook,HttpSession session){
//        Long userId = (Long) session.getAttribute("user");
        Long userId = null;
        try {
            userId = (Long) session.getAttribute("userId");
        } catch (ClassCastException e) {
            // 处理类型转换异常
        }
        addressBook.setUserId(userId);
        addressBook.setCreateUser(userId);
        addressBook.setUpdateUser(userId);
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBookService.edit(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(BigDecimal ids) {
        //Long Id = addressBook.getId(Id);
//        AddressBook addressBook = addressBookService.getById(id);
        //addressBook.setId(Id);
        addressBookService.delete(ids);
        return Result.success(null);
    }
}
